package cn.com.wangluotongxin.database.controller;

import cn.com.wangluotongxin.admin.model.AdminUser;
import cn.com.wangluotongxin.common.web.AjaxResult;
import cn.com.wangluotongxin.database.command.DatabaseCommandService;
import cn.com.wangluotongxin.database.eureka.form.ReleaseDatabaseForm;
import cn.com.wangluotongxin.database.service.IAdminUserService;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "网络通信-管理端 数据库微服务 相关接口")
@RestController
@RequestMapping("/inner/dtabase")
public class InnerDatabaseController {

    @Autowired
    private IAdminUserService userService;

    @Autowired
    private DatabaseCommandService commandService;

    @ApiOperation("发布")
    @PostMapping("/release")
    public AjaxResult release(@RequestBody ReleaseDatabaseForm form) {
        Assert.notNull(form.getSyncDatabaseId(), "缺少发布数据信息ID");
        Assert.notNull(form.getDatabaseType(), "缺少数据库类型");
        Assert.isTrue(StrUtil.isNotBlank(form.getDomain()), "缺少域名");
        Assert.notNull(form.getPort(), "缺少端口");
        Assert.isTrue(StrUtil.isNotBlank(form.getUsername()), "缺少账号");
        Assert.isTrue(StrUtil.isNotBlank(form.getPassword()), "缺少密码");
        Assert.notNull(form.getUserId(), "缺少操作人ID");
        AdminUser adminUser = userService.getById(form.getUserId());
        Assert.notNull(adminUser, "操作人不存在");

        commandService.releaseDatabase(form.getDatabaseType(), form.getSyncDatabaseId(), form.getDomain(), form.getPort(), form.getDatabaseName(), form.getUsername(), form.getPassword(), adminUser.getId(), adminUser.getUsername(), adminUser.getChannelId());
        return AjaxResult.success();
    }
}
